package com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dante.a17376060_wanghaikang_finaltest.R;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.domain.Kinds;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.useraction.KindsActivity;

import java.util.List;

public class KindsAdapter extends RecyclerView.Adapter<KindsAdapter.ViewHolder> {
    private Context mContext;
    private List<Kinds> mKinds;
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if(mContext==null){
            mContext=viewGroup.getContext();
        }
        View view= LayoutInflater.from(mContext).inflate(R.layout.kinds_item,viewGroup,false);
        final ViewHolder holder=new ViewHolder(view);
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position=holder.getAdapterPosition();
                Kinds kinds=mKinds.get(position);
                Intent intent=new Intent(mContext,KindsActivity.class);
                intent.putExtra(KindsActivity.KINDS_NAME,kinds.getKinds());
                mContext.startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        Kinds kinds=mKinds.get(i);
        viewHolder.newsKinds.setText(kinds.getKinds());
        viewHolder.newsImage.setImageResource(kinds.getKinds_image());
    }

    @Override
    public int getItemCount() {
        return mKinds.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        CardView cardView;
        ImageView newsImage;
        TextView newsKinds;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView= (CardView) itemView;
            newsImage=itemView.findViewById(R.id.news_kinds_image);
            newsKinds=itemView.findViewById(R.id.news_kinds);
        }
    }
    public KindsAdapter(List<Kinds> kindsList){
        mKinds=kindsList;
    }
}
